package cn.yangliu.comm.config;

import cn.yangliu.comm.CommConst;
import cn.yangliu.comm.components.RedisLock;
import cn.yangliu.comm.tools.SpringContextUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@ConditionalOnProperty(value = "enable", matchIfMissing = true)
public class CommConfig {
    @Bean
    public Executor executor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CommConst.EXECUTOR_CORE_POOL_SIZE);
        executor.setMaxPoolSize(CommConst.EXECUTOR_MAX_POOL_SIZE);
        executor.setQueueCapacity(CommConst.EXECUTOR_QUEUE_CAPACITY);
        executor.setThreadNamePrefix("custom-executor");
        executor.initialize();
        return executor;
    }

    @Bean
    public SpringContextUtils springUtils(){
        return new SpringContextUtils();
    }

    @Bean
    @ConditionalOnClass(RedisTemplate.class)
    public RedisLock redisLock(){
        return new RedisLock();
    }
}
